昨天我們學會怎麼在 Cloud Run Service 上建立 AI Agent,但文件上說 Vertex AI 能夠由 Google Cloud 完美地管理,因此今天來試著將 AI Agent 部署到 Vertex AI Agent Engine 上。
今天參考的文件是 Agent Development Kit,但看不懂專案架構,因此又去參考 Quickstart: Develop and deploy agents on Vertex AI Agent Engine,才發現原來這些程式碼是連貫的,要放在同個 Python 檔案內,所以大致上的結構如下:
# ==== init ====
import vertexai
from vertexai import agent_engines
vertexai.init(...)
# ==== agent ====
from google.adk.agents import Agent
from vertexai.preview.reasoning_engines import AdkApp
agent = Agent(...)
app = AdkApp(agent=agent)
# ==== test ====
for event in app.stream_query(...):
print(event)
這樣就可以在本地執行,而遠端的結構則是:
# ==== init ====
...
# ==== agent ====
...
app = AdkApp(agent=agent)
# ==== deploy ====
from vertexai import agent_engines
remote_agent = agent_engines.create(app, ...)
# ==== remote test ====
for event in remote_agent.stream_query(...):
print(event)
在執行之前要先到 Buckets 頁面建立一個新的,設定的名稱要填回 staging_bucket
參數裡(這邊的名稱蠻容易撞的,要想個獨一無二的名字):
要注意 agent_engines.create
只要執行一次就會建立一次,要小心不要一直執行它。但我為了讓程式碼整潔,把昨天的 multi_tool_agent
當作 Python package 包起來取代 agent 變數:
# ==== init ====
...
# ==== agent ====
from multi_tool_agent.agent import root_agent
agent = root_agent
# ==== deploy ====
...
在本地測試的時候沒問題,但是當要部屬到 Vertex AI Agent Engine 上時就出問題了,指令回傳的錯誤為:
google.api_core.exceptions.InvalidArgument: 400 Reasoning Engine resource [...] failed to start and cannot serve traffic. Please refer to our documentation (https://cloud.google.com/vertex-ai/generative-ai/docs/agent-engine/troubleshooting/deploy) for checking logs and other troubleshooting tips. 3: Reasoning Engine resource [...] failed to start and cannot serve traffic. Please refer to our documentation (https://cloud.google.com/vertex-ai/generative-ai/docs/agent-engine/troubleshooting/deploy) for checking logs and other troubleshooting tips.
這個時候可以到 Logs Explorer 找問題,就會看到有很多 Error message :
說他找不到我的 multi_tool_agent
,如果到剛剛建立的 Bucket 裡面會看到這些:
看起來剛剛寫的程式碼會被序列化存到 Bucket 內,但因為我的 multi_tool_agent
是一個 Package,且沒有設定相依性就導致我的 Agent 沒被上傳上去。
雖然成功將 Agent 部署到 Vertex AI 上了,但是自己寫的 Agent 就沒有辦法 CD,明天再看看文件有沒有什麼方法能夠做到。